package gin_http

import (
	"github.com/gin-gonic/gin"
	"strings"
)

// OperateLogOption 操作日志选项
type OperateLogOption struct {
	OperateLogHandler gin.HandlerFunc //指定日志记录的handler
}

func (opt OperateLogOption) OptLogHandler(defaults ...gin.HandlerFunc) gin.HandlerFunc {
	if opt.OperateLogHandler != nil {
		return opt.OperateLogHandler
	}

	if len(defaults) > 0 {
		return defaults[0]
	}

	return EmptyHandlerFunc
}

func (opt OperateLogOption) RecordLog(s ...string) gin.HandlerFunc {
	var log = strings.Join(s, ",")
	var h = opt.OptLogHandler()
	return func(ctx *gin.Context) {
		SetOperateLog(ctx, log) //自定义设置日志内容
		h(ctx)
	}
}
